<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_job_ads extends Access_securities {

	public function __construct(){
		parent::__construct("control-job-ads");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Manage Employers";
		$data['job_ads']=$this->model_globals->select_all('job_ads')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_job_ads(){
		$data['title']="Create Job ads";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$this->form_validation->set_rules('category', 'Job Category', 'required');
		$this->form_validation->set_rules('location', 'Location', 'required');
		$this->form_validation->set_rules('job_terms', 'Job Terms', 'required');
		$this->form_validation->set_rules('title', 'Job Title', 'required');
		$this->form_validation->set_rules('date_available', 'Date Available', 'required');
		$this->form_validation->set_rules('contact_person', 'Contact Person', 'required');
		$this->form_validation->set_rules('company', 'Company Name', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories = $this -> model_globals -> select_all('job_categories') -> result();
			$option = $this->get_job_category_options($categories);
			$data['categories']=$option;
			$data['job_types']=$this->model_globals->select_all('job_types');
			$data['job_terms']=$this->model_globals->select_all('job_term');
			$data['edu_levels']=$this->model_globals->select_all('education_levels');
			$data['contact_persons']=$this->model_globals->select_all('contact_persons');
			$data['companies']=$this->model_globals->select_all('companies')->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			//$token_key="";
			$splite_category=explode("-",$this->input->post('category'));
			$date_available_input = explode("/", $this -> input -> post('date_available'));
			$date_available_db = $date_available_input[2] . "-" . $date_available_input[1] . "-" . $date_available_input[0];
			$job_key = random_string('alnum', 12);
			while ($this -> isExist($job_key)) {
				$job_key = random_string('alnum', 12);
			}
			$data=array(
				'using_job_category_id'=>$splite_category[0],
				'using_job_sub_category_id'=>$splite_category[1],
				'using_job_location_id'=>$this->input->post('location'),
				'using_job_type_id'=>$this->input->post('job_type'),
				'using_contact_person_id'=>$this->input->post('contact_person'),
				'using_job_term_id'=>$this->input->post('job_terms'),
				'using_education_level_id'=>$this->input->post('edu_level'),
				'title'=>$this->input->post('title'),
				'salary'=>$this->input->post('salary'),
				'age'=>$this->input->post('age'),
				'hiring'=>$this->input->post('num_hiring'),
				'date_available'=>$date_available_db,
				'text_description'=>$this->input->post('description'),
				'text_requirements'=>$this->input->post('requirement'),
				'posted_by'=>'admin',
				'using_company_id'=>$this->input->post('company'),
				'notification'=>$this->input->post('notification'),
				'job_keys'=>$job_key,
				'gender'=>$this->input->post('gender'),
				'status'=>$this->input->post('active'),
				'created_date'=>null
			);
			
			$is_inserted_job_ads=$this->model_globals->insert('job_ads',$data);
			if($is_inserted_job_ads){
				$this -> session -> set_flashdata('success', 'New job ads  was posted success !.');
				redirect('control-job-ads');
			}else{
				$this -> session -> set_flashdata('error', 'New job ads  was posted fail !.');
				redirect('control-job-ads');
			}
		}
	}
	public function update_job_ads(){
		$data['title']="Modify Job ads";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$job_ads_id=$this->uri->segment(3);
		
		$this->form_validation->set_rules('category', 'Job Category', 'required');
		$this->form_validation->set_rules('location', 'Location', 'required');
		$this->form_validation->set_rules('job_terms', 'Job Terms', 'required');
		$this->form_validation->set_rules('title', 'Job Title', 'required');
		$this->form_validation->set_rules('date_available', 'Date Available', 'required');
		$this->form_validation->set_rules('contact_person', 'Contact Person', 'required');
		$this->form_validation->set_rules('company', 'Company Name', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['job_ads_info']=$this->model_globals->select_where('job_ads',array('job_ads_id'=>$job_ads_id));
			//$job_ads_sub_category=$data['job_ads_info']->row()->using_job_sub_category_id;
			$using_job_category_id=$data['job_ads_info']->row()->using_job_sub_category_id;
			$using_main_job_category_id=$data['job_ads_info']->row()->using_job_category_id;
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories = $this -> model_globals -> select_all('job_categories') -> result();
				$option = "";
				foreach ($categories as $category) {
					$category_id = $category -> job_category_id;
					$name = $category -> name;
					
					
					if($using_main_job_category_id.'-'.$using_job_category_id==$category_id.'-0'){
						$selected_main_category="selected";
						
					}else{
						$selected_main_category="";
					}
					
					$option .= '<option value="' . $category_id . '-0"'.$selected_main_category.' style="color:#3C8DBC;">' . $name . '</option>';
					//$option .= '<optgroup label="' . $name . '">';
					$sub_categories = $this -> model_globals -> select_where('job_sub_categories', array('using_job_category_id' => $category_id)) -> result();
					foreach ($sub_categories as $sub_category) {
						$sub_category_id = $sub_category -> job_sub_category_id;
						$sub_name = $sub_category -> name;
						
						$job_category_selected=(($category_id.'-'.$using_job_category_id)==($category_id.'-'.$sub_category_id))? 'selected':'';
						$option .= '<option value="' . $category_id . '-' . $sub_category_id . '"'.$job_category_selected.'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $sub_name . '</option>';
					}
					//$option .= '</optgroup>';
				}
			$data['categories']=$option;
			$data['job_types']=$this->model_globals->select_all('job_types');
			$data['job_terms']=$this->model_globals->select_all('job_term');
			$data['edu_levels']=$this->model_globals->select_all('education_levels');
			$data['contact_persons']=$this->model_globals->select_all('contact_persons');
			$data['companies']=$this->model_globals->select_all('companies')->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			$token_key="";
			$splite_category=explode("-",$this->input->post('category'));
			$date_available_input = explode("/", $this -> input -> post('date_available'));
			$date_available_db = $date_available_input[2] . "-" . $date_available_input[1] . "-" . $date_available_input[0];
			$data=array(
				'using_job_category_id'=>$splite_category[0],
				'using_job_sub_category_id'=>$splite_category[1],
				'using_job_location_id'=>$this->input->post('location'),
				'using_job_type_id'=>$this->input->post('job_type'),
				'using_contact_person_id'=>$this->input->post('contact_person'),
				'using_job_term_id'=>$this->input->post('job_terms'),
				'using_education_level_id'=>$this->input->post('edu_level'),
				'title'=>$this->input->post('title'),
				'salary'=>$this->input->post('salary'),
				'age'=>$this->input->post('age'),
				'hiring'=>$this->input->post('num_hiring'),
				'date_available'=>$date_available_db,
				'text_description'=>$this->input->post('description'),
				'text_requirements'=>$this->input->post('requirement'),
				'notification'=>$this->input->post('notification'),
				'gender'=>$this->input->post('gender'),
				'status'=>$this->input->post('active'),
				'using_company_id'=>$this->input->post('company'),
				//'created_date'=>null
			);
			
			$is_updated_job_ads=$this->model_globals->update('job_ads',$data,array('job_ads_id'=>$job_ads_id));
			if($is_updated_job_ads){
				$this -> session -> set_flashdata('success', 'Job ads  was modified success !.');
				redirect('control-job-ads');
			}else{
				$this -> session -> set_flashdata('error', 'Job ads  was modified fail !.');
				redirect('control-job-ads');
			}
		}
		
	}
	public function delete_job_ads(){
		$job_ads_id=$this->uri->segment(3);
		$is_deleted_job_ads=$this->model_globals->delete('job_ads',array('job_ads_id'=>$job_ads_id));
		if($is_deleted_job_ads){
			$this -> session -> set_flashdata('success', 'Job ads  was deleted success !.');
			redirect('control-job-ads');
		}else{
			$this -> session -> set_flashdata('error', 'Job ads  was deleted fail !.');
			redirect('control-job-ads');
		}
	}
	public function disabled_job_ads(){
		$data['title']="Disabled Employer";
		$this->load->view(B_MASTER."index");
	}
	public function enabled_job_ads(){
		$data['title']="Enabled Employer";
		$this->load->view(B_MASTER."index");
	}
	public function get_job_category_options($categories){
		$option='';
		foreach ($categories as $category) {
			$category_id = $category -> job_category_id;
			$name = $category -> name;
			//$option .= '<optgroup label="' . $name . '">';
			$option .= '<option value="' . $category_id . '-0 " style="color:#3C8DBC;">' . $name . '</option>';
			$sub_categories = $this -> model_globals -> select_where('job_sub_categories', array('using_job_category_id' => $category_id)) -> result();
			foreach ($sub_categories as $sub_category) {
				$sub_category_id = $sub_category -> job_sub_category_id;
				$sub_name = $sub_category -> name;
				$option .= '<option value="' . $category_id . '-' . $sub_category_id . '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $sub_name . '</option>';
			}
			//$option .= '</optgroup>';
		}
		return $option;
		
	}
	function isExist($job_key) {
		if (!$this -> model_globals -> check_data_exist("job_ads", array("job_keys" => $job_key))) {
			return FALSE;
		} else {
			return TRUE;
		}
	}
	
}
